/**
 * author:Administrator
 * date:2016/2/26
 * desc: 用户注册和登录
 * 1. 创建角色
 * 2. 根据登录结果，查找一个角色
 */

'use strict';
//
let authService = require('../infrastructure/service/authService.js');
let FormatOutput = require('../infrastructure/appUtil/FormatOutput.js');
let ShopkeeperRepo = require('../domain/model_cmd/shopkeeper/ShopkeeperRepo.js');

//
let service = {};
module.exports = service;

//
let ROLE_ENUM = authService.ROLE_ENUM;
let STATUS_ENUM = authService.STATUS_ENUM;

/**
 *  注册一个 customer
 */
let Customer = require('../domain/model_cmd/customer/Customer.js');
let CustomerRepo = require('../domain/model_cmd/customer/CustomerRepo.js');
service.registerCustomer = function*(name, pwd) {
  let user = yield authService.register(name, pwd, ROLE_ENUM.customer);
  if (!user) return false;
  let customer = new Customer({name: name, uId: user.id});
  yield CustomerRepo.store(customer);
  return customer;
};


/**
 * 注册一个 Vendor
 */
let Vendor = require('../domain/model_cmd/vendor/Vendor.js');
let VendorRepo = require('../domain/model_cmd/vendor/VendorRepo.js');
service.registerVendor = function*(name, pwd) {
  let user = yield authService.register(name, pwd, ROLE_ENUM.vendor);
  if (!user) return false;
  let vendor = new Vendor({name: name, uId: user.id});
  yield VendorRepo.store(vendor);
  return vendor;
};


/**
 * 注册一个 Manager
 */
let Manager = require('../domain/model_cmd/manager/Manager.js');
let ManagerRepo = require('../domain/model_cmd/manager/ManagerRepo.js');
service.registerManager = function*(name, pwd) {
  let user = yield authService.register(name, pwd, ROLE_ENUM.manager);
  if (!user) return false;
  let manager = new Manager({name: name, uId: user.id});
  yield ManagerRepo.store(manager);
  return manager;
};


/**
 * 用户登录,返回一个角色
 * @param name
 * @param pwd
 */
service.login = function*(name, pwd) {
  //console.log('roleService::login ', name, pwd);
  let user = yield authService.login(name, pwd);
  //console.log('user ', user);
  return yield userToRole(user);
};


/**
 * 用户=>角色
 * 一个用户 可能有多个角色
 * @param user
 */
function *userToRole(user) {
  //console.log('roleService::userToRole user=', user);
  if (!user) return null;

  let role;
  if (user.role == ROLE_ENUM.customer) {
    role = yield CustomerRepo.findByUid(user.id);
    //console.log('customer role=', role);
    role = role && role[0];
    if (role) role.userType = 'customer';
  } else if (user.role == ROLE_ENUM.vendor) {
    role = yield VendorRepo.findByUid(user.id);
    //console.log('vendor role=', role);
    role = role && role[0];
    if (role) role.userType = 'vendor';
  } else if (user.role == ROLE_ENUM.manager) {
    role = yield ManagerRepo.findByUid(user.id);
    //console.log('manager role=', role);
    role = role && role[0];
    if (role) role.userType = 'manager';
  } else if (user.role == ROLE_ENUM.shopkeeper) {
    role = yield ShopkeeperRepo.findByUid(user.id);
    //console.log('keeper role=', role);
    role = role && role[0];
    if (role) role.userType = 'shopkeeper';
  }
  //console.log('roleService::userToRole role=', role);
  return role;

  //let roles = {};
  //roles.customers = yield CustomerRepo.findByUid(user.id);
  //roles.vendors = yield VendorRepo.findByUid(user.id);
  //roles.managers = yield ManagerRepo.findByUid(user.id);
  //roles.keepers = yield ShopkeeperRepo.findByUid(user.id);
  //
  //return roles;
}
